﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;

namespace Devonline.Identity.Admin.Controllers;

/// <summary>
/// 组织部门管理
/// </summary>    
[Route("api/[controller]")]
[ApiController]
[SecurityHeaders]
[Authorize(Roles = GROUP_MAINTAINERS)]
public class GroupsController : ControllerBase
{
    private readonly IDataService<IdentityDbContext, Group> _dataService;
    public GroupsController(IDataService<IdentityDbContext, Group> dataService) => _dataService = dataService;

    /// <summary>
    /// 查询组织单位
    /// </summary>
    /// <returns></returns>
    [HttpGet, EnableQuery]
    public IActionResult Get() => Ok(_dataService.GetQueryable());

    [HttpPost]
    public async Task<IActionResult> CreateAsync(GroupViewModel entitySet) => Ok(await _dataService.AddAsync<GroupViewModel, Attachment>(entitySet, default));

    [HttpPut]
    public async Task<IActionResult> UpdateAsync(GroupViewModel entitySet) => Ok(await _dataService.UpdateAsync<GroupViewModel, Attachment>(entitySet, default, false));

    [HttpDelete("{id}")]
    public async Task<IActionResult> DeleteAsync(string id)
    {
        await _dataService.DeleteAsync<Group, Attachment>(id, default, false);
        return Ok();
    }
}